/**
 *
 * © Bahadir Zeren 2011
 * bzeren@gmail.com
 * No warranties, express or implied, are made for this program.
 *
 */

package org.baatar.net.test;

import org.apache.log4j.Logger;
import org.baatar.net.message.IRequest;
import org.baatar.net.message.IResponse;
import org.baatar.net.tcp.IMessageProcessor;

/**
 * Processor class of the Math TCP Service. Gets and processes calculation
 * requests and sends the response back.
 * 
 * @author Ba
 */
public class MathProcessor implements IMessageProcessor {

    @Override
    public IResponse processMessage(String clientIP,
                                    int clientPortNr,
                                    IRequest request,
                                    Logger logger) throws Exception {
        Request req = (Request) request;
        double a = req.getA();
        double b = req.getB();
        String operation = req.getOperation();

        double res = 0.0;
        if (operation.equals("+"))
            res = a + b;
        else
            if (operation.equals("-"))
                res = a - b;
            else
                if (operation.equals("*"))
                    res = a * b;
                else
                    if (operation.equals("/"))
                        res = a / b;

        logger.debug(req.getClientInfo() + "-" + clientIP + ":" + clientPortNr + " : process " + a + " " + operation + " " + b + " = " + res);

        return new Response(res);
    }

}
